package com.unipv14.androidgallery;

import listeners.FileItemListener;
import listeners.IListener;
import listeners.ItemListener;
import listeners.PlayListListener;
import playlistData.PlaylistManager;
import android.app.Activity;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import fileManagers.FileListGetter;

public class ListsRefresher implements IListsRefresher {
	private Activity mainActivity;
	private FileListGetter getter;
	private int fileListID;
	private int playlistID;
	private PlaylistManager playlistManager;

	public ListsRefresher(Activity mainActivity, FileListGetter getter,
			int fileListID, int playlistID, PlaylistManager playlistManager) {
		super();
		this.mainActivity = mainActivity;
		this.getter = getter;
		this.fileListID = fileListID;
		this.playlistID = playlistID;
		this.playlistManager = playlistManager;
	}

	@Override
	public void refreshList() {

		ListView list = (ListView) mainActivity.findViewById(fileListID);
		String[] files = getter.getList();

		ArrayAdapter<String> adapter = new ArrayAdapter<String>(mainActivity,
				android.R.layout.simple_list_item_1, files);

		list.setAdapter(adapter);
		list.setLongClickable(true);

		IListener listener = new FileItemListener(mainActivity, this, getter,
				playlistManager);

		ItemListener itemListener = new ItemListener(listener,this,mainActivity);

		list.setOnItemClickListener(itemListener);
		list.setOnItemLongClickListener(itemListener);

		list = (ListView) mainActivity.findViewById(playlistID);

		adapter = new ArrayAdapter<String>(mainActivity,
				android.R.layout.simple_list_item_1, playlistManager.getMap()
						.getNames());
		list.setAdapter(adapter);

		IListener playListener = new PlayListListener(mainActivity, this,
				playlistManager);

		itemListener = new ItemListener(playListener,this,mainActivity);

		list.setLongClickable(true);
		list.setOnItemClickListener(itemListener);
		list.setOnItemLongClickListener(itemListener);
	}

}
